package bavaria.hightech.logging;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Formatter;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import bavaria.hightech.banking.Bank;
import bavaria.hightech.banking.BankAdministration;
import bavaria.hightech.banking.BankCustomerSight;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;


public class DebugHandler implements InvocationHandler  {
    private Bank bank;
    private static Logger logger;
    private FileHandler handler;
    
    public DebugHandler(Bank bank) throws SecurityException, IOException  {
        this.bank = bank;
		logger = Logger.getLogger(DebugHandler.class.getName());
		//String filePathLogger = System.getProperty("java.class.path") +"bavaria\\hightech\\logging\\banklog.txt";
		handler = new FileHandler("banklog.txt");
		handler.setFormatter(new SimpleFormatter());
		logger.addHandler(handler);
    }
    
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable  {
        Object result = null;
        String logString = "";
        logString += "* calling method " + method + " with params ";
        if(args == null){
    		logString += " no params";
    	}else{
    		for (int i = 0; i < args.length; i++){
                logString += " " + args[i];
            }
    	}
        System.out.println(logString);
        
        try  {
            result = method.invoke(bank, args);
        } catch(IllegalAccessException ex)  {
            ex.printStackTrace();
        } catch(InvocationTargetException ex)  {
            System.out.println("* exception:" + ex.getTargetException());
            ex.getTargetException().printStackTrace();
            throw ex.getTargetException();
        }
        System.out.println("* result:" + result);
        logger.fine(logString +"\n" +"* result:" + result +"\n");
        return result;
    }
}